<template>
  <div class="home-container">
    <el-row :gutter="20">
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>总用户数</span>
              <el-icon>
                <User />
              </el-icon>
            </div>
          </template>
          <div class="card-content">
            <span class="number">{{ userCount || 0 }}</span>
            <span class="trend">
              <!-- 暂时移除增长率显示 -->
            </span>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>今日访问</span>
              <el-icon>
                <View />
              </el-icon>
            </div>
          </template>
          <div class="card-content">
            <span class="number">0</span>
            <span class="trend">
              <!-- 暂时移除增长率显示 -->
            </span>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>订单总数</span>
              <el-icon>
                <ShoppingCart />
              </el-icon>
            </div>
          </template>
          <div class="card-content">
            <span class="number">0</span>
            <span class="trend">
              <!-- 暂时移除增长率显示 -->
            </span>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>总收入</span>
              <el-icon>
                <Money />
              </el-icon>
            </div>
          </template>
          <div class="card-content">
            <span class="number">￥0</span>
            <span class="trend">
              <!-- 暂时移除增长率显示 -->
            </span>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="mt-20">
      <el-col :span="16">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>访问趋势</span>
            </div>
          </template>
          <div class="chart-container">
            <!-- 这里可以集成图表库，如ECharts -->
            <div class="placeholder-chart">图表区域</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>系统公告</span>
            </div>
          </template>
          <div v-if="notices.length === 0" class="empty-notice">
            暂无公告
          </div>
          <el-timeline v-else>
            <el-timeline-item v-for="(notice, index) in notices" :key="notice.id" :timestamp="notice.createTime"
              :type="getNoticeType(notice.type)">
              <div class="notice-item" @click="handleViewNotice(notice)">
                {{ notice.title }}
              </div>
            </el-timeline-item>
          </el-timeline>
        </el-card>
      </el-col>
    </el-row>

    <!-- 公告详情对话框 -->
    <el-dialog title="公告详情" v-model="noticeDialog.visible" width="700px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="公告标题" :span="2">{{ noticeDialog.data.title }}</el-descriptions-item>
        <el-descriptions-item label="公告类型">
          <el-tag :type="noticeDialog.data.type === '1' ? 'warning' : 'success'" effect="light">
            {{ noticeDialog.data.type === '1' ? '通知' : '公告' }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag :type="noticeDialog.data.status === '1' ? 'success' : 'info'" effect="light">
            {{ noticeDialog.data.status === '1' ? '正常' : '关闭' }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="创建者">{{ noticeDialog.data.creator }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ noticeDialog.data.createTime }}</el-descriptions-item>
        <el-descriptions-item label="公告内容" :span="2">
          <div class="notice-content">{{ noticeDialog.data.content }}</div>
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { User, View, ShoppingCart, Money } from '@element-plus/icons-vue'
import { getUserList } from '@/api/user'
import { getAllNotices } from '@/api/notice'

// 用户数量
const userCount = ref(0)

// 公告列表
const notices = ref([])

// 公告详情对话框
const noticeDialog = reactive({
  visible: false,
  data: {}
})

// 获取用户数量
const fetchUserCount = async () => {
  try {
    const res = await getUserList({ pageNum: 1, pageSize: 10 })
    console.log("获取用户数据", res);

    if (res.success) {
      userCount.value = res.data.total
    }
  } catch (error) {
    console.error('获取用户数据失败：', error)
  }
}

// 获取公告列表
const fetchNotices = async () => {
  try {
    const res = await getAllNotices()
    if (res.success) {
      // 只显示状态为正常的公告，并限制数量
      notices.value = res.data
        .filter(notice => notice.status === '1')
        .slice(0, 5)
    }
  } catch (error) {
    console.error('获取公告列表失败：', error)
  }
}

// 将公告类型转换为时间线项的类型
const getNoticeType = (type) => {
  // 类型1为通知，类型2为公告
  return type === '1' ? 'warning' : 'success'
}

// 查看公告详情
const handleViewNotice = (notice) => {
  noticeDialog.data = notice
  noticeDialog.visible = true
}

onMounted(() => {
  // 页面加载时获取数据
  fetchUserCount()
  fetchNotices()
})
</script>

<style scoped>
.home-container {
  padding: 20px;
}

.mt-20 {
  margin-top: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.number {
  font-size: 24px;
  font-weight: bold;
  color: #303133;
}

.trend {
  font-size: 14px;
}

.trend.up {
  color: #67C23A;
}

.trend.down {
  color: #F56C6C;
}

.chart-container {
  height: 300px;
}

.placeholder-chart {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f7fa;
  border-radius: 4px;
  color: #909399;
}

.empty-notice {
  text-align: center;
  color: #909399;
  padding: 20px 0;
}

.notice-item {
  cursor: pointer;
}

.notice-item:hover {
  color: #409EFF;
}

.notice-content {
  white-space: pre-wrap;
  line-height: 1.5;
}
</style>